##rk: rearrange to group by event with submitters under that 
#
##rk: reject => (delete event?), leave submitter_event with rejected flag 
##rk: accept => leave submitter_event with accepted flag 
##rk: generic display (by default), all non-accepted, non-destroyed entries ==> also allows for stats: # of ([un]valid) submissions per-user 

class EventsSubmittersController < ApplicationController
  before_filter :authorize, :except => :login       ## is it unsafe to have this be in administration (e.g. can it be called externally)?
  before_filter :setup                              ## is it unsafe to have this be in administration (e.g. can it be called externally)?

  def changeable                      ##rk: need to put this in views to display edit link or not..
    # events are not changeable once accepted/rejected
    @events_submitter = EventsSubmitter.find(params[:id])
    return true   ## unless @events_submitter.accepted || @events_submitter.rejected
  end

  # GET /events_submitters
  # GET /events_submitters.xml
  def index
    @events_submitter = EventsSubmitter.find(:all, :conditions => { :submitter_id => @user.id })

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @events_submitter }
    end
  end

  # GET /events_submitters/review/[1]
  # GET /events_submitters/review/1.xml
  def review
    if params[:user_id]
      @events_submitter = EventsSubmitter.find(:all, :conditions => { :submitter_id => params[:user_id] })
      @submitter = Submitter.find(params[:user_id])
    else
      @events_submitter = EventsSubmitter.find(:all)
      @submitter = nil
    end

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @events_submitter } if params[:user_id]
    end
  end

  # GET /events_submitters/1
  # GET /events_submitters/1.xml
  def show
    @events_submitter = EventsSubmitter.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @events_submitter }
    end
  end

  # GET /events_submitters/new
  # GET /events_submitters/new.xml
  def new
    @events_submitter = EventsSubmitter.new

    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @events_submitter }
    end
  end

  # GET /events_submitters/1/edit
  def edit
    @events_submitter = EventsSubmitter.find(params[:id])
  end

  # POST /events_submitters
  # POST /events_submitters.xml
  def create
    @events_submitter = EventsSubmitter.new(params[:events_submitter])

    s = []
    s << "<p>Must enter two team names for game #{gn}</p>" if !params[:teamA] || params[:teamA].empty? || !params[:teamB] || params[:teamB].empty?
    s << "<p>Must enter league names for each league</p>" if !params[:leagueA] || params[:leagueA].empty? || !params[:leagueB] || params[:leagueB].empty?
    s << "<p>Must enter an event name</p>" if !params[:leagueA] || params[:leagueA].empty? || !params[:leagueB] || params[:leagueB].empty?
    s << "<p>Must enter a score for each team</p>" if !params[:scoreA] || params[:scoreA].empty? || !params[:scoreB] || params[:scoreB].empty?
    s << "<p>Scores must be numeric</p>" unless (params[:scoreA] =~ /[0-9]+/) && (params[:scoreB] =~ /[0-9]+/) 
    unless (s.empty?)
      flash[:notice] = s
      render :action => "new"
      return
    end

    gn = 1
    team_names = [ params[:teamA], params[:teamB] ]

    teams = []
    team_names.each do |t|
      team = Team.find(t) # :all, :conditions => [ "name like ?", '%' + t + '%' ])            ## if drop down, likely will be id
      if (!team || team.length > 1)
        flash[:notice] = "Cannot find team matching #{t} for game #{gn}"
        render :action => "new"
        return
      end
      teams << team[0]
    end

    ##rk: Leagues not necessary yet..
    league_names = [ params[:leagueA], params[:leagueB]]

    leagues  = []
    league_names.each do |l|
      league = League.find(l) # .find_by_name(l)            ## if drop down, likely will be id
      if (!league) ##|| league.length > 1)
        flash[:notice] = "Cannot find league matching #{l} for game #{gn}"
        render :action => "new"
        return
      end
      leagues << league[0]
    end

    event = Event.create(:name => params[:event], :start_date => params[:start], :end_date => params[:end])
    game = Game.new(:name => teams[0].name + " vs. " + teams[1].name)
    scoreA = Score.new(:points => params[:scoreA], :identifier => teams[0].name, :score_type => "team")
    scoreB = Score.new(:points => params[:scoreB], :identifier => teams[1].name, :score_type => "team")

    game.teams << teams[0]
    game.teams << teams[1]

    game.scores << scoreA
    game.scores << scoreB

    event.games << game

    scoreA.save()
    scoreB.save()
    game.save()
    event.save()

    submitter = Submitter.find(@user)
##    event.submitters << submitter                   ## this creates a new row in the join table (then we save ours below...)
    @events_submitter.event_id = event.id
    @events_submitter.submitter_id = submitter.id

    respond_to do |format|
      if @events_submitter.save
        flash[:notice] = "#{params[:event]} was successfully created."
        format.html { redirect_to(@events_submitter) }
        format.xml  { render :xml => @events_submitter, :status => :created, :location => @events_submitter }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @events_submitter.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /events_submitters/1
  # PUT /events_submitters/1.xml
  def update
    @events_submitter = EventsSubmitter.find(params[:id])

    respond_to do |format|
      if @events_submitter.update_attributes(params[:events_submitter])
        flash[:notice] = 'EventsSubmitter was successfully updated.'
        format.html { redirect_to(@events_submitter) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @events_submitter.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /events_submitters/1
  # DELETE /events_submitters/1.xml
  def destroy
    @events_submitter = EventsSubmitter.find(params[:id])
    @events_submitter.destroy

    respond_to do |format|
      format.html { redirect_to(events_submitters_url) }
      format.xml  { head :ok }
    end
  end
end

# vim:ts=2:et:sw=2
